﻿using System;
using System.Data;
using System.Configuration;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using System.Data.SqlClient;

/// <summary>
/// UserDAO 的摘要说明
/// </summary>
public class UserDAO
{
    SqlHelp help = new SqlHelp();
    string sql = string.Empty;
	public UserDAO()
	{
		//
		// TODO: 在此处添加构造函数逻辑
		//
	}

    public int AddUser(UserModel model)
    {
        sql = " Insert Into B_User(UserCode, UserName, UserPwd, UserFullName, UserEngName, UserPost, LawEntity, Email, BackupEmail, DomainName,  "
            + "DeleteFlag, AddAuthor, AddDate, UpdateAuthor, UpdateDate) values(@UserCode, @UserName, @UserPwd, @UserFullName, "
            + "@UserEngName, @UserPost, @LawEntity, @Email, @BackupEmail, @DomainName,  @DeleteFlag, @AddAuthor, @AddDate, @UpdateAuthor, @UpdateDate)";
        SqlParameter[] paras = new SqlParameter[]{
            new SqlParameter("@UserCode",model.UserCode),
            new SqlParameter("@UserName",model.UserName),
            new SqlParameter("@UserPwd",model.UserPwd),
            new SqlParameter("@UserFullName",model.UserFullName),
            new SqlParameter("@UserEngName",model.UserEngName),
            new SqlParameter("@UserPost",model.UserPost),
            new SqlParameter("@LawEntity",model.LawEntity),
            new SqlParameter("@Email",model.Email),
            new SqlParameter("@BackupEmail",model.BackupEmail),
            new SqlParameter("@DomainName",model.DomainName),
            new SqlParameter("@DeleteFlag",model.DeleteFlag),
            new SqlParameter("@AddAuthor",model.AddAuthor),
            new SqlParameter("@AddDate",model.date),
            new SqlParameter("@UpdateAuthor",model.AddAuthor),
            new SqlParameter("@UpdateDate",model.date)
        };
        return help.RunSqlInt(sql, paras);
    }

    public int EditUser(UserModel model)
    {
        sql = " update B_User set UserName=@UserName, UserFullName=@UserFullName, "
            +"UserEngName=@UserEngName, UserPost=@UserPost, LawEntity=@LawEntity, Email=@Email, BackupEmail=@BackupEmail, DomainName=@DomainName,  "
            + "DeleteFlag=@DeleteFlag, UpdateAuthor=@UpdateAuthor, UpdateDate=@UpdateDate where UserCode=@UserCode ";
        SqlParameter[] paras = new SqlParameter[]{
            new SqlParameter("@UserCode",model.UserCode),
            new SqlParameter("@UserName",model.UserName),
            new SqlParameter("@UserFullName",model.UserFullName),
            new SqlParameter("@UserEngName",model.UserEngName),
            new SqlParameter("@UserPost",model.UserPost),
            new SqlParameter("@LawEntity",model.LawEntity),
            new SqlParameter("@Email",model.Email),
            new SqlParameter("@BackupEmail",model.BackupEmail),
            new SqlParameter("@DomainName",model.DomainName),
            new SqlParameter("@DeleteFlag",model.DeleteFlag),
            new SqlParameter("@UpdateAuthor",model.UpdateAuthor),
            new SqlParameter("@UpdateDate",model.date)
        };
        return help.RunSqlInt(sql, paras);
    }

    public int DeleteUser(UserModel model)
    {
        SqlParameter[] paras = new SqlParameter[] { new SqlParameter("@UserCode", model.UserCode) };
        return help.RunProcInt("DeleteUser", paras);
    }

    public int updatePWD(string userCode)
    {
        sql = string.Format(" update B_User set UserPwd='{0}' where UserCode='{1}'", Login.EncryptPassword("111111", Login.PasswordType.MD5), userCode);
        return help.RunSqlInt(sql);
    }

    /// <summary>
    ///  绑定单个用户信息
    /// </summary>
    public DataTable GetOneUserInfo(string userCode)
    {
        DataTable dt = new DataTable();
        string sql = "select *,case deleteFlag when '0' then '启用-Active' when '1' then '停用-Inactive' end as aa from B_User U where userCode='" + userCode + "'";
        help.RunSQL(sql, ref dt);
        return dt;
    }

    /// <summary>
    ///  绑定用户信息
    /// </summary>
    public DataTable GetUserInfo(string selectSql)
    {
        DataTable dt = new DataTable();
        string sql = "select U.* from B_User U where U.deleteFlag!='2' ";
        sql += selectSql;
        help.RunSQL(sql, ref dt);
        return dt;
    }

    /// <summary>
    ///  绑定用户信息(Excel)
    /// </summary>
    public DataTable GetExcelUserInfo(string selectSql)
    {
        DataTable dt = new DataTable();
        string sql = "select UserCode,UserName,UserEngName,UserFullName,case U.DeleteFlag when '0' then '启用-Active' when '1' then '停用-Inactive' end as status,Email,U.UserDesc from B_User U where U.deleteFlag!='2' " + selectSql + " order by usercode";
        help.RunSQL(sql, ref dt);
        return dt;
    }
}
